/* Create treatment and weights */
use "Raw Data/AC_geo_sample.dta", clear

gen camp_lag1 = poll_date-station_date1
gen camp_lag2 = poll_date-station_date2
gen camp_lag3 = poll_date-station_date3

gen eligible_1 = (camp_lag1==2)
gen eligible_2 = (camp_lag2==2)
gen eligible_3 = (camp_lag3==2)

gen eligibleearly_1 = (camp_lag1>2  & camp_lag1!=.)
gen eligibleearly_2 = (camp_lag2>2  & camp_lag2!=.)
gen eligibleearly_3 = (camp_lag3>2  & camp_lag3!=.)

drop if eligibleearly_1==1 | eligibleearly_2 ==1 | eligibleearly_3==1

drop if eligible_1==0 & eligible_2==0 & eligible_3==0

gen treat_1 = (station_bdcast1 == 1 & eligible_1 == 1)
gen treat_2 = (station_bdcast2 == 1 & eligible_2 == 1)
gen treat_3 = (station_bdcast3 == 1 & eligible_3 == 1)

gen treat = (treat_1==1|treat_2==1|treat_3==1)

egen num_stations = rsum(eligible_?)

gen prob_treat = 0
replace prob_treat = 0.5 if num_stations == 1
replace prob_treat = 0.75 if num_stations == 2
replace prob_treat = 0.875 if num_stations == 3

gen wgt_treat = treat/prob_treat + (1 - treat)/(1 - prob_treat)

keep  state_name pc_num ac_num rural_pc lit_pc scst_pc state_election poll_date station_id1 station_id2 station_id3 num_stations treat wgt_treat prob_treat

save "Analysis Data/AC_est_sample.dta", replace

keep state_name pc_num
bys state_name pc_num: drop if _n>1

save "Analysis Data/PC_est_sample.dta", replace
 